<?php
/**
 * ads zone控制器
 *
 * @package     Include  (controller)
 * @author      hary <hary@taomee.com>
 * @version     $ID 2010-3-31 $
 */
class Admin_zone extends Controller
{
	private $result;

	/**
	 * 构造函数
	 */
	function __construct()
	{
		parent::__construct();

		$this->loadModel('ads_zone_model');

		$this->loadClass('Mcache');
		$this->tpl->template_dir = SYS_PATH  . 'template/admin/zone/';
	}

	/**
	 * 默认执行方法
	 * @return max
	 */
	function index()
	{
		$this->getZones();
	}

	/**
	 * 获取zone列表
	 * @return max
	 */
	function getZones(){

		$page = max(1,$this->in_var['page']);
		$this->result['zones'] = $this->ads_zone_model->getAdsZonesByKeywords('',$page,5);
		
		$page_info = $this->ads_zone_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}'), 3);

		$this->tpl->assign("page_info",$page_info);
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("zones.html");
	}

	/**
	 * 编辑选取 
	 * @return max
	 */
	function editZone(){
		$item = $this->in_var['item'];


		$zone  = $this->ads_zone_model->getAdsZoneById($item);

		$this->tpl->assign("zone",$zone);
		$this->tpl->display("zone.html");
	}

	/**
	 * 添加选取
	 * @return max
	 */
	function addZone(){

		$this->tpl->assign("result",$this->result);
		$this->tpl->display("zone.html");
	}
	
	/**
	 * 获取代码
	 * @return max
	 */
	function getScript(){
		
		$this->tpl->template_dir = SYS_PATH  . 'template/web/script/';
		
		$key = $this->in_var['key'];
		
		$this->loadModel("ads_banner_model");
		
		$banner = $this->ads_banner_model->getAdsBannerByZoneKey($key);
		
		$this->tpl->assign("banner",$banner);
		
		$this->tpl->display("preview.html");
		
	}

	/**
	 * 根据条件查询zone
	 * @return max
	 */
	function searchZones(){

		$page     = max(1,$this->in_var['page']);
		$keywords = $this->in_var['keywords'];

		$this->result['zones'] = $this->ads_zone_model->getAdsZonesByKeywords($keywords,$page,5);

		$page_info = $this->ads_zone_model->getPageInfo('/admin'.get_url(strtolower(__CLASS__), __FUNCTION__, 'page={Page}','keywords='.$this->in_var['keywords']), 3);

		$this->tpl->assign('page_info',$page_info);
		$this->tpl->assign("result",$this->result);
		$this->tpl->display("zones.html");
	}

	/**
	 * 保存zone
	 * @return max
	 */
	function saveZone(){

		$flag = false;
		$zone = $this->in_var['zone'];

		$ad_key = $zone['z_key'];

		if(!empty($ad_key))
		{
			$flag = $this->ads_zone_model->changeAdsZoneByKey($zone);
			
			//清除文件缓存
			$this->tpl->template_dir = SYS_PATH  . 'template/web/formwork/';
			$this->loadModel("ads_banner_model");
			$banner = $this->ads_banner_model->getBannerByZone($zone['z_id']);
			$this->tpl->clear_cache($banner['b_mtype']['mt_formwork_name'],$banner['b_id']);
			//memcache缓存清空
			$this->Mcache->clearCache("ads",'zones');
		}
		else
		{
			$flag = $this->ads_zone_model->addAdsZone($zone);
		}

		exit(json_encode($flag?array('info'=>$this->sys_conf_var['ads']['customer_msg']['zone']['save_success'],'status'=>true):array('info'=>$this->sys_conf_var['ads']['customer_msg']['zone']['save_error'],'status'=>false)));
	}

	/**
	 * 删除zone
	 * @return max
	 */
	function delZones(){

		$ids = $this->in_var['item'];
		$id_ = explode(',',$ids);

		try{
			foreach($id_ as $id)
			{
				$this->ads_zone_model->delAdsZoneById($id);
			}
		}
		catch(Exception $e)
		{
			exit(json_encode(array('info'=>$this->sys_conf_var['ads']['customer_msg']['zone']['del_error'],'status'=>false)));
		}

		exit(json_encode(array('info'=>$this->sys_conf_var['ads']['customer_msg']['zone']['del_success'],'status'=>true)));
	}

}